package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.db.LLFloorDBManager;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.db.LLSceneDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.SelectRoomFloorAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectFloorRoomActivity extends AppActivity {
    private RecyclerView list;
    private SelectRoomFloorAdapter adapter;
    private int selectType;//0：所属楼层；1：自动生成选择房间
    private TextView btnSave;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_floor_room;
    }

    @Override
    protected void initView() {
        selectType = getInt("selectType",0);
        String title = getString("title");
        if(!TextUtils.isEmpty(title)){
            setTitle(title);
        }
        list = findViewById(R.id.list);
        btnSave = findViewById(R.id.btn_start);
        list.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new SelectRoomFloorAdapter(getContext(), false, selectType != 1);
        list.setAdapter(adapter);
        setOnClickListener(R.id.btn_start);
        if(selectType != 1){
            setRightTitle(R.string.save);
            btnSave.setVisibility(View.GONE);
        }else{
            setRightTitle("");
            btnSave.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initData() {
        List<FloorBean> floorBeans = LLFloorDBManager.getInstance(getContext()).getAllDetailWithCommonRoom(selectType);
        if(selectType == 0  && floorBeans.size() != 1){
            for(int i = 1;i < floorBeans.size();i++){
                FloorBean floorBean = floorBeans.get(i);
                RoomBean roomBean = new RoomBean();
                roomBean.setRoomName(floorBean.getFloorName());
                roomBean.setRoomSerialNo(floorBean.getFloorSerialNo());
                roomBean.setFloorSerialNo(floorBean.getFloorSerialNo());
                roomBean.setFloorName(floorBean.getFloorName());
                List<DeviceBean> devices = LLDeviceDBManager.getInstance(getContext()).getHomeAvailableDeviceByFloor(floorBean.getFloorSerialNo());
                List<SceneBean> scenes = LLSceneDBManager.getInstance(getContext()).getSceneByFloor(floorBean.getFloorSerialNo());
                roomBean.setDeviceSize(devices.size());
                roomBean.setSceneSize(scenes.size());
                floorBean.getRooms().add(0,roomBean);
            }
        }
        adapter.setData(floorBeans);
        String roomSerialNo = getString("roomSerialNo");
        int linkType = getInt("linkType");
        if(linkType == 3){
            Map<String, RoomBean> map = new HashMap<>();
            FloorBean floorBean = LLFloorDBManager.getInstance(getContext()).getFloorByFloorSerialNo(roomSerialNo);
            if(floorBeans != null && floorBeans.size() > 0 && floorBean != null){
                RoomBean roomBean = new RoomBean();
                roomBean.setRoomSerialNo(roomSerialNo);
                roomBean.setFloorName(floorBean.getFloorName());
                roomBean.setFloorSerialNo(floorBean.getFloorSerialNo());
                map.put(roomSerialNo, roomBean);
                adapter.initSelectMap(map);
            }
        }else if(RoomBean.ROOM_COMMON_SERIALNO.equals(roomSerialNo) || linkType == 2){
            Map<String, RoomBean> map = new HashMap<>();
            if(floorBeans != null && floorBeans.size() > 0){
                List<RoomBean> rooms = floorBeans.get(0).getRooms();
                if(rooms != null && rooms.size() > 0){
                    map.put(rooms.get(0).getRoomSerialNo(), rooms.get(0));
                    adapter.initSelectMap(map);
                }
            }
        }else{
            if(!TextUtils.isEmpty(roomSerialNo)){
                RoomBean roomBean = LLRoomDBManager.getInstance(getContext()).getRoomBySerialNo(roomSerialNo);
                if(roomBean != null){
                    Map<String, RoomBean> map = new HashMap<>();
                    map.put(roomBean.getRoomSerialNo(), roomBean);
                    adapter.initSelectMap(map);
                }
            }
        }
    }

    @Override
    public void onRightClick(View view) {
        if(adapter.getSelectedData() == null || adapter.getSelectedData().size() == 0){
            toast(R.string.please_select_room_hint);
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("serialNo", adapter.getSelectedData().get(0).getRoomSerialNo());
        intent.putExtra("roomSerialNo", adapter.getSelectedData().get(0).getRoomSerialNo());
        intent.putExtra("roomName", adapter.getSelectedData().get(0).getRoomName());
        if(adapter.getSelectedData().get(0).getIsDeafultFloor()){
            intent.putExtra("floorSerialNo","");
            intent.putExtra("floorName", "");
        }else{
            intent.putExtra("floorSerialNo", adapter.getSelectedData().get(0).getFloorSerialNo());
            intent.putExtra("floorName", adapter.getSelectedData().get(0).getFloorName());
        }
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                if(adapter.getSelectedData() == null || adapter.getSelectedData().size() == 0){
                    toast(R.string.please_select_room_hint);
                    return;
                }
                Intent intent = new Intent(this,SceneAutoCreateActivity.class);
                List<Map> rooms = new ArrayList<>();
                for(RoomBean roomBean : adapter.getSelectedData()){
                    Map<String,Object> map = new HashMap<>();
                    if(roomBean.getRoomSerialNo().equals("00000000-0000-0000-0000-000000000000")){
                        map.put("roomType","0");
                        map.put("roomSerialNo","");
                        map.put("floorSerialNo","");
                        map.put("floorName","");
                        map.put("name","全屋");
                        map.put("linkType","2");
                    }else{
                        map.put("roomType",roomBean.getRoomTypeId());
                        map.put("roomSerialNo",roomBean.getRoomSerialNo());
                        map.put("name",roomBean.getRoomName());
                        map.put("floorSerialNo",roomBean.getFloorSerialNo());
                        map.put("floorName",roomBean.getFloorName());
                        map.put("linkType","1");
                    }

                    rooms.add(map);
                }
                intent.putExtra("rooms",(Serializable) rooms);
                startActivity(intent);
                break;
        }
    }
}